//
//  DepoistQRCodeVC.swift
//  d11
//
//  Created by peale on 2018/11/1.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class DepoistQRCodeVC: BXBaseVC {
    var bgImageView:UIImageView?
    var frameImageView:UIImageView?
    var qrcodeImageView:UIImageView?
     var titleLabel:UILabel?
    var subTitleLabel:UILabel?
    var hintLabel:UILabel?
    var line1View :UIView?
    var line2View :UIView?
    var continueBtn:UIButton?
    var homeBtn:UIButton?
    var shareBtn:UIButton?
    var urlString : String?
    var titleString : String?
    var msgString : String?
    var amtString : String?
    lazy var countDown: MCCountDown = {
        let c = MCCountDown()
        return c
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
         self.title = ""
        let topLogo = UIImage(named: "icon_nav_left_logo")
        let titleView = UIView()
        let topLogoView = UIImageView(image: topLogo)
        titleView.addSubview(topLogoView)
        topLogoView.contentMode = .scaleToFill
        topLogoView.frame = CGRect(x: -(137/2), y:-(35/2), width: 137, height: 35)
        navigationItem.titleView = titleView
        self.view.backgroundColor = HexRGBAlpha(0xECECEC, 1)
        self.createSubView()
    }
    
    func createSubView() -> Void {
        self.titleLabel = UILabel.init()
        self.titleLabel?.text = self.titleString
        self.titleLabel?.textColor = UIColor.black
        self.titleLabel?.textAlignment = .center
        self.titleLabel?.backgroundColor = UIColor.clear
        self.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        self.view?.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
            make.top.equalToSuperview()
        }
        
        self.bgImageView = UIImageView.init()
       // self.bgImageView?.image = imageNamed("icon_depoist_qrcode_bg")
        self.bgImageView?.layer.masksToBounds = true
        self.bgImageView?.layer.cornerRadius = 3
        self.bgImageView?.layer.borderWidth = 1
        self.bgImageView?.layer.borderColor = HexRGBAlpha(0xd4d4d4, 1).cgColor
        self.bgImageView?.backgroundColor = UIColor.white
        self.view.addSubview(self.bgImageView!)
        self.bgImageView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.height.equalTo((self.view.snp_width)).offset(30)
            make.top.equalTo((self.titleLabel?.snp.bottom)!)
        }
        
        self.frameImageView = UIImageView.init()
        QRCodeUtil.setQRCodeToImageView(self.frameImageView,self.urlString)
        self.bgImageView?.addSubview(self.frameImageView!)
        self.frameImageView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-40)
            make.centerX.equalToSuperview()
            make.height.equalTo((self.bgImageView?.snp.width)!).offset(-40)
            make.top.equalToSuperview()
        }
        self.line1View = UIView.init()
        self.line1View?.backgroundColor = HexRGBAlpha(0xd4d4d4, 1)
        self.bgImageView?.addSubview(self.line1View!)
        self.line1View?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(((self.frameImageView?.snp.bottom)!))
        }
        
        self.subTitleLabel =  UILabel.init()
        self.subTitleLabel?.text = "金额:\(self.amtString ?? "0.00")元"
        self.subTitleLabel?.attributedText = self.subTitleLabel?.attributedTextColorWithString(string: self.subTitleLabel?.text! as! NSString, subString: "\(self.amtString ?? "0.00")元" as NSString, color: UIColor.red)
        self.subTitleLabel?.textColor = UIColor.black
        self.subTitleLabel?.textAlignment = .center
        self.subTitleLabel?.backgroundColor = UIColor.clear
        self.subTitleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.view?.addSubview(self.subTitleLabel!)
        self.subTitleLabel?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
            make.top.equalTo(((self.line1View?.snp.bottom)!))
        }
        
        self.line2View = UIView.init()
        self.line2View?.backgroundColor = HexRGBAlpha(0xd4d4d4, 1)
        self.bgImageView?.addSubview(self.line2View!)
        self.line2View?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(((self.subTitleLabel?.snp.bottom)!))
        }
        
        self.hintLabel =  UILabel.init()
        self.hintLabel?.text = "温馨提示:\(self.msgString ?? "")"
        self.hintLabel?.textColor = UIColor.red
        self.hintLabel?.textAlignment = .center
        self.hintLabel?.backgroundColor = UIColor.clear
        self.hintLabel?.font = UIFont.systemFont(ofSize: 14)
        self.view?.addSubview(self.hintLabel!)
        self.hintLabel?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
            make.top.equalTo(((self.line2View?.snp.bottom)!))
        }
        
        self.shareBtn  = UIButton.init(type: .custom)
        self.shareBtn?.setTitle("立即跳转", for: .normal)
   //     self.shareBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        //self.shareBtn?.setTitleColor(UIColor.white, for: .normal)
        let layerGradient = CAGradientLayer()
        layerGradient.colors = [
            UIColor.init(red: 1, green: 0.78, blue: 0.45, alpha: 1).cgColor,
            UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        ]
        layerGradient.frame = CGRect(x: 0, y: 0, width:kScreenWidth-40, height: 40)
        shareBtn?.layer.addSublayer(layerGradient)
        self.shareBtn?.layer.cornerRadius = 5;
        self.shareBtn?.layer.masksToBounds = true
//        self.shareBtn?.layer.borderWidth = 1
//        self.shareBtn?.layer.borderColor = HexRGBAlpha(0xFFCF8D, 1).cgColor
       // self.shareBtn?.backgroundColor = UIColor.clear
        self.shareBtn?.addTarget(self, action: #selector(self.actionToDepoistShare), for: .touchUpInside)
        self.view.addSubview(self.shareBtn!)
        self.shareBtn?.snp.makeConstraints { (make) in
            make.width.equalTo((kScreenWidth-40))
            make.height.equalTo(40)
            make.centerX.equalTo(self.view.snp.centerX)
            make.top.equalTo((self.bgImageView?.snp.bottom)!).offset(20)
        }
//        self.titleLabel = UILabel.init()
//        self.titleLabel?.text = "00:05:00"
//        self.titleLabel?.textColor = HexRGBAlpha(0x666666, 1)
//        self.titleLabel?.textAlignment = .center
//        self.titleLabel?.backgroundColor = UIColor.clear
//        self.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
//        self.view?.addSubview(self.titleLabel!)
//        let formatter = DateFormatter()
//        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//        let startDate = Date()
//        let endDate = startDate.adjust(.minute, offset: 5)
       // self.titleLabel?.startTimeStr = formatter.string(from: startDate)
    //    self.titleLabel?.endTimeStr = formatter.string(from: endDate)
        
//        let time = countDown.openCountdown(start: formatter.string(from: startDate), end: formatter.string(from: endDate), format: "yyyy-MM-dd HH:mm:ss")
//        showCountDown(time: time)
//
//        countDown.closure = {time in
//
//            self.showCountDown(time: time)
//        }
//        self.titleLabel?.snp.makeConstraints{(make) in
//            make.width.equalToSuperview()
//            make.centerX.equalToSuperview()
//            make.height.equalTo(30)
//            make.top.equalTo((self.bgImageView?.snp.bottom)!).offset(10)
//        }
        
//        self.continueBtn  = UIButton.init(type: .custom)
//        self.continueBtn?.setTitle("继续存款", for: .normal)
//        self.continueBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
//        self.continueBtn?.setTitleColor(UIColor.white, for: .normal)
//        self.continueBtn?.setBackgroundImage(imageNamed("icon_depoist_checking_btn_bg"), for: .normal)
//        self.continueBtn?.layer.cornerRadius = 5;
//        self.continueBtn?.layer.masksToBounds = true
//        self.continueBtn?.addTarget(self, action: #selector(self.actionToDepoistContinue), for: .touchUpInside)
//        self.view.addSubview(self.continueBtn!)
//        self.continueBtn?.snp.makeConstraints { (make) in
//            make.width.equalTo((kScreenWidth-60)/2)
//            make.height.equalTo(40)
//            make.right.equalTo(self.view.snp.centerX).offset(-10)
//            make.top.equalTo((self.titleLabel?.snp.bottom)!).offset(20)
//        }
//
//        self.homeBtn  = UIButton.init(type: .custom)
//        self.homeBtn?.setTitle("返回上级", for: .normal)
//        self.homeBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
//        self.homeBtn?.setBackgroundImage(imageNamed("icon_depoist_checking_btn_bg2"), for: .normal)
//        self.homeBtn?.setTitleColor(UIColor.white, for: .normal)
//        self.homeBtn?.layer.cornerRadius = 5;
//        self.homeBtn?.layer.masksToBounds = true
//        self.homeBtn?.addTarget(self, action: #selector(self.actionToDepoistHome), for: .touchUpInside)
//        self.view.addSubview(self.homeBtn!)
//        self.homeBtn?.snp.makeConstraints { (make) in
//            make.width.equalTo((kScreenWidth-60)/2)
//            make.height.equalTo(40)
//            make.left.equalTo(self.view.snp.centerX).offset(10)
//            make.top.equalTo((self.titleLabel?.snp.bottom)!).offset(20)
//        }
        
//        self.shareBtn  = UIButton.init(type: .custom)
//        self.shareBtn?.setTitle("分享", for: .normal)
//        self.shareBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
//        self.shareBtn?.setTitleColor(HexRGBAlpha(0xFFCF8D, 1), for: .normal)
//        self.shareBtn?.layer.cornerRadius = 5;
//        self.shareBtn?.layer.masksToBounds = true
//        self.shareBtn?.layer.borderWidth = 1
//        self.shareBtn?.layer.borderColor = HexRGBAlpha(0xFFCF8D, 1).cgColor
//        self.shareBtn?.backgroundColor = UIColor.clear
//        self.shareBtn?.addTarget(self, action: #selector(self.actionToDepoistShare), for: .touchUpInside)
//        self.view.addSubview(self.shareBtn!)
//        self.shareBtn?.snp.makeConstraints { (make) in
//            make.width.equalTo((kScreenWidth-40))
//            make.height.equalTo(40)
//            make.centerX.equalTo(self.view.snp.centerX)
//            make.top.equalTo((self.homeBtn?.snp.bottom)!).offset(20)
//        }
        
    }
    
    @objc func actionToDepoistContinue() -> Void {
        
        
    }
    
    @objc func actionToDepoistHome() -> Void {
        self.navigationController?.popToRootViewController(animated: true)
    }
    
    @objc func actionToDepoistShare() -> Void {
        let urlstr = self.urlString ?? ""
        let encodeUrlString = urlstr.addingPercentEncoding(withAllowedCharacters:
               .urlQueryAllowed)
        if let url = URL(string: encodeUrlString!){
            if UIApplication.shared.canOpenURL(url){
                //根据iOS系统版本，分别处理
                if #available(iOS 10, *) {
                    UIApplication.shared.open(url, options: [:],
                                              completionHandler: {
                                                (success) in
                    })
                } else {
                    UIApplication.shared.openURL(url)
                }
            }else{
                //因必须安装支付宝app才能进行操作 所以还是给予提示不处理跳转到网页
                UIHelper.alertSimpleMessage(about: "请下载支付宝app.", withDescription: "")
//               let h5UrlString = "https://ds.alipay.com/?scheme="+(encodeUrlString ?? "")
//                if let h5Url = URL(string: h5UrlString){
//                  if UIApplication.shared.canOpenURL(h5Url){
//                        //根据iOS系统版本，分别处理
//                                 if #available(iOS 10, *) {
//                                     UIApplication.shared.open(h5Url, options: [:],
//                                                               completionHandler: {
//                                                                 (success) in
//                                     })
//                                 } else {
//                                     UIApplication.shared.openURL(h5Url)
//                                 }
//                    }else{
//                        UIHelper.alertSimpleMessage(about: "请下载支付宝app.", withDescription: "")
//                    }
//                  }
            }

        }
        
//      let vc =  WebViewController.init()
//        vc.url = urlstr
//        self.navigationController?.pushViewController(vc, animated: true)
//        if let url = URL(string: urlstr), UIApplication.shared.canOpenURL(url) {
//            print("可以")
//            //根据iOS系统版本，分别处理
//            if #available(iOS 10, *) {
//                UIApplication.shared.open(url, options: [:],
//                                          completionHandler: {
//                                            (success) in
//                })
//            } else {
//                UIApplication.shared.openURL(url)
//            }
//        }
        
    }
    func showCountDown(time:MCCountDownStruct) {
        let h:String?
        if time.hour  < 10{
            h = "0\(time.hour)"
        }else{
             h = "\(time.hour)"
        }
     
        let m:String?
        if time.minit  < 10{
            m = "0\(time.minit)"
        }else{
            m = "\(time.minit)"
        }
        
        let s:String?
        if time.second  < 10{
            s = "0\(time.second)"
        }else{
            s = "\(time.second)"
        }
        self.titleLabel?.text = "\(h ?? "00"):\(m ?? "00"):\(s ?? "00")"
    }
}

